<?php
namespace app\admin\controller;

class Module extends Base
{
    private $tn = 'module';

    private $type = ['0' => '后台模块', '1' => '基础模块'];

    public function index()
    {
        if (isset($this->get['page']) && isset($this->get['limit'])) {
            $where = ['id' => ['<>', 1]];
            $list  = db($this->tn)->field(true)->where($where)->page($this->get['page'])->limit($this->get['limit'])->select();
            foreach ($list as $k => $v) {
                $list[$k]['type'] = $this->type[$v['type']];
            }
            $count = db($this->tn)->where($where)->count();
            $this->returnAPI('', 0, $list, ['count' => $count, 'limit' => $this->get['limit']]);
        }
        return $this->fetch();
    }

    public function edit($id = 0)
    {
        $model = model($this->tn);
        if ($this->post) {
            if (isset($this->post['prefix'])) {
                if (strpos($this->post['prefix'], '_') !== strlen($this->post['prefix']) - 1) {
                    $this->post['prefix'] .= '_';
                }
            }
            if ($id) {
                $model = $model->get($id);
                if (is_null($model)) {
                    $this->returnAPI('信息有误，请稍后重试');
                }
            }
            $result = $model->validate($this->tn)->allowField(true)->save($this->post);
            if ($result !== false) {
                if (!$id) {
                    $module_path = APP_PATH . $this->post['route'];
                    @mkdir($module_path);
                    @mkdir($module_path . DS . 'controller');
                    @mkdir($module_path . DS . 'extra');
                    @mkdir($module_path . DS . 'view');
                    $template_module = ROOT_PATH . 'extend' . DS . 'template' . DS . 'module' . DS;
                    @file_put_contents($module_path . DS . 'extra' . DS . 'database.php', str_replace(['{{$prefix}}'], [$this->post['prefix']], file_get_contents($template_module . 'database.php.tpl')));
                    if ($this->post['type']) {
                        @file_put_contents($module_path . DS . 'view' . DS . 'layout.html', file_get_contents($template_module . 'layout.base.html.tpl'));
                        @file_put_contents($module_path . DS . 'controller' . DS . 'Base.php', str_replace(['{{$module}}'], [$this->post['route']], file_get_contents($template_module . 'base.php.tpl')));
                    } else {
                        @mkdir($module_path . DS . 'model');
                        @mkdir($module_path . DS . 'validate');
                        @mkdir(ROOT_PATH . 'public' . DS . 'static' . DS . 'module' . DS . $this->post['route']);
                        @file_put_contents($module_path . DS . 'view' . DS . 'layout.html', file_get_contents($template_module . 'layout.html.tpl'));
                        @file_put_contents($module_path . DS . 'controller' . DS . 'Base.php', str_replace(['{{$module}}'], [$this->post['route']], file_get_contents($template_module . 'base.auth.php.tpl')));
                    }
                }
                $this->returnAPI('操作成功', 0);
            }
            $this->returnAPI($model->getError());
        }
        $model = $model->get($id);
        if (is_null($model)) {
            $data = $this->table_fields($this->tn);
        } else {
            $data = $model->getData();
        }
        return $this->fetch('', ['data' => $data, 'type' => $this->type]);
    }

    public function remove()
    {
        $model = model($this->tn)->get($this->post['id']);
        if (is_null($model)) {
            $result = true;
        } else {
            $result = $model->delete();
            if ($result) {
                delete_dir(APP_PATH . $model->route);
                if (!$model->type) {
                    delete_dir(ROOT_PATH . 'public' . DS . 'static' . DS . 'module' . DS . $model->route);
                }
            }
        }
        if ($result) {
            $this->returnAPI('操作成功', 0);
        }
        $this->returnAPI('操作失败,请稍后重试');
    }
}
